home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / operat_1 / picmask.frm (.txt) next >
Encoding:
Visual Basic Form  |  1999-08-25  |  23.7 KB  |  638 lines

  1. VERSION 5.00
  2. Begin VB.Form frmPicMask 
  3.    BorderStyle     =   1  'Fixed Single
  4.    Caption         =   "Mask, transparency and raster operations"
  5.    ClientHeight    =   5595
  6.    ClientLeft      =   45
  7.    ClientTop       =   330
  8.    ClientWidth     =   6645
  9.    Icon            =   "picmask.frx":0000
  10.    LinkTopic       =   "Form1"
  11.    LockControls    =   -1  'True
  12.    MaxButton       =   0   'False
  13.    MinButton       =   0   'False
  14.    ScaleHeight     =   373
  15.    ScaleMode       =   3  'Pixel
  16.    ScaleWidth      =   443
  17.    Begin VB.CommandButton cmdClear 
  18.       Caption         =   "Clear"
  19.       BeginProperty Font 
  20.          Name            =   "MS Sans Serif"
  21.          Size            =   8.25
  22.          Charset         =   0
  23.          Weight          =   700
  24.          Underline       =   0   'False
  25.          Italic          =   0   'False
  26.          Strikethrough   =   0   'False
  27.       EndProperty
  28.       Height          =   375
  29.       Left            =   3300
  30.       TabIndex        =   19
  31.       ToolTipText     =   "Use only one API, via reverse masked foreground"
  32.       Top             =   4830
  33.       Width           =   1020
  34.    End
  35.    Begin VB.CommandButton cmdExit 
  36.       Caption         =   "Exit"
  37.       BeginProperty Font 
  38.          Name            =   "MS Sans Serif"
  39.          Size            =   8.25
  40.          Charset         =   0
  41.          Weight          =   700
  42.          Underline       =   0   'False
  43.          Italic          =   0   'False
  44.          Strikethrough   =   0   'False
  45.       EndProperty
  46.       Height          =   375
  47.       Left            =   4350
  48.       TabIndex        =   18
  49.       Top             =   4830
  50.       Width           =   1020
  51.    End
  52.    Begin VB.PictureBox picTransparent 
  53.       AutoRedraw      =   -1  'True
  54.       Height          =   1575
  55.       Left            =   4650
  56.       ScaleHeight     =   101
  57.       ScaleMode       =   3  'Pixel
  58.       ScaleWidth      =   102
  59.       TabIndex        =   17
  60.       Top             =   2910
  61.       Width           =   1590
  62.    End
  63.    Begin VB.PictureBox picFgd 
  64.       AutoRedraw      =   -1  'True
  65.       Height          =   1545
  66.       Left            =   2490
  67.       Picture         =   "picmask.frx":030A
  68.       ScaleHeight     =   99
  69.       ScaleMode       =   3  'Pixel
  70.       ScaleWidth      =   102
  71.       TabIndex        =   16
  72.       Top             =   630
  73.       Width           =   1590
  74.    End
  75.    Begin VB.CommandButton cmdMethod2 
  76.       Caption         =   "Method 2"
  77.       BeginProperty Font 
  78.          Name            =   "MS Sans Serif"
  79.          Size            =   8.25
  80.          Charset         =   0
  81.          Weight          =   700
  82.          Underline       =   0   'False
  83.          Italic          =   0   'False
  84.          Strikethrough   =   0   'False
  85.       EndProperty
  86.       Height          =   375
  87.       Left            =   2250
  88.       TabIndex        =   14
  89.       ToolTipText     =   "Involve many APIs and DCs, via transparent bitmap"
  90.       Top             =   4830
  91.       Width           =   1020
  92.    End
  93.    Begin VB.PictureBox picReverseMaskedFgd 
  94.       AutoRedraw      =   -1  'True
  95.       Height          =   1545
  96.       Left            =   4650
  97.       ScaleHeight     =   99
  98.       ScaleMode       =   3  'Pixel
  99.       ScaleWidth      =   104
  100.       TabIndex        =   5
  101.       Top             =   2910
  102.       Width           =   1620
  103.    End
  104.    Begin VB.CommandButton cmdMethod1 
  105.       Caption         =   "Method 1"
  106.       BeginProperty Font 
  107.          Name            =   "MS Sans Serif"
  108.          Size            =   8.25
  109.          Charset         =   0
  110.          Weight          =   700
  111.          Underline       =   0   'False
  112.          Italic          =   0   'False
  113.          Strikethrough   =   0   'False
  114.       EndProperty
  115.       Height          =   375
  116.       Left            =   1200
  117.       TabIndex        =   4
  118.       ToolTipText     =   "Use only one API, via reverse masked foreground"
  119.       Top             =   4830
  120.       Width           =   1020
  121.    End
  122.    Begin VB.PictureBox picProduct 
  123.       AutoRedraw      =   -1  'True
  124.       Height          =   1575
  125.       Left            =   4710
  126.       ScaleHeight     =   101
  127.       ScaleMode       =   3  'Pixel
  128.       ScaleWidth      =   102
  129.       TabIndex        =   3
  130.       Top             =   630
  131.       Width           =   1590
  132.    End
  133.    Begin VB.PictureBox picReversedMask 
  134.       AutoRedraw      =   -1  'True
  135.       Height          =   1515
  136.       Left            =   2520
  137.       ScaleHeight     =   97
  138.       ScaleMode       =   3  'Pixel
  139.       ScaleWidth      =   98
  140.       TabIndex        =   2
  141.       Top             =   2910
  142.       Width           =   1530
  143.    End
  144.    Begin VB.PictureBox picMask 
  145.       AutoRedraw      =   -1  'True
  146.       Height          =   1575
  147.       Left            =   270
  148.       ScaleHeight     =   101
  149.       ScaleMode       =   3  'Pixel
  150.       ScaleWidth      =   102
  151.       TabIndex        =   1
  152.       Top             =   2880
  153.       Width           =   1590
  154.    End
  155.    Begin VB.PictureBox picBackgd 
  156.       AutoRedraw      =   -1  'True
  157.       AutoSize        =   -1  'True
  158.       Height          =   1650
  159.       Left            =   270
  160.       Picture         =   "picmask.frx":7FFC
  161.       ScaleHeight     =   106
  162.       ScaleMode       =   3  'Pixel
  163.       ScaleWidth      =   102
  164.       TabIndex        =   0
  165.       Top             =   630
  166.       Width           =   1590
  167.    End
  168.    Begin VB.PictureBox picUnblockedFgd 
  169.       AutoRedraw      =   -1  'True
  170.       Height          =   1545
  171.       Left            =   2520
  172.       ScaleHeight     =   99
  173.       ScaleMode       =   3  'Pixel
  174.       ScaleWidth      =   99
  175.       TabIndex        =   15
  176.       Top             =   2910
  177.       Width           =   1545
  178.    End
  179.    Begin VB.Label lblEqual 
  180.       Caption         =   "="
  181.       BeginProperty Font 
  182.          Name            =   "MS Sans Serif"
  183.          Size            =   13.5
  184.          Charset         =   0
  185.          Weight          =   700
  186.          Underline       =   0   'False
  187.          Italic          =   0   'False
  188.          Strikethrough   =   0   'False
  189.       EndProperty
  190.       Height          =   315
  191.       Left            =   4290
  192.       TabIndex        =   13
  193.       Top             =   1170
  194.       Width           =   225
  195.    End
  196.    Begin VB.Label lblPlus 
  197.       Caption         =   "+"
  198.       BeginProperty Font 
  199.          Name            =   "MS Sans Serif"
  200.          Size            =   13.5
  201.          Charset         =   0
  202.          Weight          =   700
  203.          Underline       =   0   'False
  204.          Italic          =   0   'False
  205.          Strikethrough   =   0   'False
  206.       EndProperty
  207.       Height          =   315
  208.       Left            =   2040
  209.       TabIndex        =   12
  210.       Top             =   1200
  211.       Width           =   225
  212.    End
  213.    Begin VB.Label lblEndResult 
  214.       Alignment       =   2  'Center
  215.       Caption         =   "Product"
  216.       BeginProperty Font 
  217.          Name            =   "MS Sans Serif"
  218.          Size            =   8.25
  219.          Charset         =   0
  220.          Weight          =   700
  221.          Underline       =   0   'False
  222.          Italic          =   0   'False
  223.          Strikethrough   =   0   'False
  224.       EndProperty
  225.       Height          =   255
  226.       Left            =   4860
  227.       TabIndex        =   11
  228.       Top             =   240
  229.       Width           =   1380
  230.    End
  231.    Begin VB.Label lblReversedMask 
  232.       Alignment       =   2  'Center
  233.       Caption         =   "Reversed mask"
  234.       Height          =   375
  235.       Left            =   2670
  236.       TabIndex        =   10
  237.       Top             =   2460
  238.       Width           =   1230
  239.    End
  240.    Begin VB.Label lblReverseMaskedFgd 
  241.       Alignment       =   2  'Center
  242.       Caption         =   "Reverse-masked foreground"
  243.       Height          =   405
  244.       Left            =   4800
  245.       TabIndex        =   9
  246.       Top             =   2460
  247.       Width           =   1350
  248.    End
  249.    Begin VB.Label lblBackGround 
  250.       Alignment       =   2  'Center
  251.       Caption         =   "Background"
  252.       BeginProperty Font 
  253.          Name            =   "MS Sans Serif"
  254.          Size            =   8.25
  255.          Charset         =   0
  256.          Weight          =   700
  257.          Underline       =   0   'False
  258.          Italic          =   0   'False
  259.          Strikethrough   =   0   'False
  260.       EndProperty
  261.       Height          =   255
  262.       Left            =   480
  263.       TabIndex        =   8
  264.       Top             =   240
  265.       Width           =   1020
  266.    End
  267.    Begin VB.Label lblMask 
  268.       Alignment       =   2  'Center
  269.       Caption         =   "Mask"
  270.       Height          =   285
  271.       Left            =   510
  272.       TabIndex        =   7
  273.       Top             =   2550
  274.       Width           =   1050
  275.    End
  276.    Begin VB.Label lblForeGround 
  277.       Alignment       =   2  'Center
  278.       Caption         =   "Foreground"
  279.       BeginProperty Font 
  280.          Name            =   "MS Sans Serif"
  281.          Size            =   8.25
  282.          Charset         =   0
  283.          Weight          =   700
  284.          Underline       =   0   'False
  285.          Italic          =   0   'False
  286.          Strikethrough   =   0   'False
  287.       EndProperty
  288.       Height          =   255
  289.       Left            =   2790
  290.       TabIndex        =   6
  291.       Top             =   240
  292.       Width           =   1020
  293.    End
  294. Attribute VB_Name = "frmPicMask"
  295. Attribute VB_GlobalNameSpace = False
  296. Attribute VB_Creatable = False
  297. Attribute VB_PredeclaredId = True
  298. Attribute VB_Exposed = False
  299. ' PicMask.frm
  300. ' By Herman Liu
  301. ' If you have problems in grasping the basics of mask and raster operations, or
  302. ' find it difficult to digest the materials you have tried hard to obtain, it
  303. ' is not yourself to blame. Most of the materials around on these topics are either
  304. ' too "dry" or only "half-baked" for non-professionals, some are even obsolete.
  305. ' This code attempts to knit together the essential points pertaining to the
  306. ' inter-relationship of mask, transparency and raster operations, in a live example.
  307. ' Hope it will be of help to you.
  308. ' The exercise is to overlay a given picture onto another; the latter being the
  309. ' background of the new picture.  Two different approaches are presented; it is up
  310. ' to you to choose which one to follow (or both).
  311. Option Explicit
  312. Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, _
  313.     ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, _
  314.     ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
  315. '--------------------------------------------------------
  316. ' The following APIs are required only when Method 2 is opted
  317. '--------------------------------------------------------
  318. Private Declare Function SetBkColor Lib "gdi32" (ByVal hdc As Long, _
  319.     ByVal crColor As Long) As Long
  320. Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, _
  321.     ByVal y As Long) As Long
  322. Private Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hdc As Long, _
  323.     ByVal nWidth As Long, ByVal nHeight As Long) As Long
  324. Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
  325. Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, _
  326.     ByVal hObject As Long) As Long
  327. Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
  328. Private Declare Function CreateBitmap Lib "gdi32" (ByVal nWidth As Long, ByVal nHeight _
  329.     As Long, ByVal nPlanes As Long, ByVal nBitCount As Long, lpBits As Any) As Long
  330. Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
  331. Dim picW As Long
  332. Dim picH As Long
  333. Dim mresult
  334.             
  335. Private Sub Form_Load()
  336.     Me.Move 0, 0
  337.     picBackgd.AutoRedraw = True
  338.     picBackgd.AutoSize = True
  339.     picFgd.AutoRedraw = True
  340.     picFgd.AutoSize = False
  341.     picMask.AutoRedraw = True
  342.     picMask.AutoSize = False
  343.     picProduct.AutoRedraw = True
  344.     picProduct.AutoSize = True
  345.       ' For use if Method 1 only
  346.     picReverseMaskedFgd.AutoRedraw = True
  347.     picReverseMaskedFgd.AutoSize = False
  348.     picReversedMask.AutoRedraw = True
  349.     picReversedMask.AutoSize = False
  350.       ' For use if Method 2 only (Not shared, for clarity purposes)
  351.     picUnblockedFgd.AutoRedraw = True
  352.     picUnblockedFgd.AutoSize = False
  353.     picTransparent.AutoRedraw = True
  354.     picTransparent.AutoSize = False
  355.     picBackgd.Width = picFgd.Width
  356.     picBackgd.Height = picFgd.Height
  357.     picMask.Width = picFgd.Width
  358.     picMask.Height = picFgd.Height
  359.     picProduct.Width = picFgd.Width
  360.     picProduct.Height = picFgd.Height
  361.     picReverseMaskedFgd.Height = picFgd.Height
  362.     picReverseMaskedFgd.Width = picFgd.Width
  363.     picReversedMask.Height = picFgd.Height
  364.     picReversedMask.Width = picFgd.Width
  365.     picUnblockedFgd.Width = picFgd.Width
  366.     picUnblockedFgd.Height = picFgd.Height
  367.     picTransparent.Width = picFgd.Width
  368.     picTransparent.Height = picFgd.Height
  369.       ' Align
  370.     picFgd.Top = picBackgd.Top
  371.     picProduct.Top = picBackgd.Top
  372.     picUnblockedFgd.Top = picMask.Top
  373.     picReverseMaskedFgd.Top = picMask.Top
  374.     picTransparent.Top = picMask.Top
  375.     picReversedMask.Top = picMask.Top
  376.     picMask.Left = picBackgd.Left
  377.     picUnblockedFgd.Left = picFgd.Left
  378.     picReverseMaskedFgd.Left = picFgd.Left
  379.     picTransparent.Left = picProduct.Left
  380.     picReversedMask.Left = picProduct.Left
  381.       ' For convenience
  382.     picW = picBackgd.ScaleWidth
  383.     picH = picBackgd.ScaleHeight
  384.       ' Default these first. The following two labels
  385.       ' are shared by Method 1 and 2.
  386.     lblMask.Caption = ""
  387.     lblReverseMaskedFgd.Caption = ""
  388.     lblReversedMask.Caption = ""
  389.     picUnblockedFgd.Visible = False
  390.     picTransparent.Visible = False
  391. End Sub
  392. ' To blacken the non-white area
  393. Sub CreateMask(inPic As PictureBox, inColorToUse)
  394.     On Error Resume Next
  395.     Dim mTranspColor As Long
  396.     Dim i, j
  397.     mTranspColor = inPic.Point(1, 1)
  398.         ' See if existing background is fully covered by
  399.         ' some foreground color which is to serve as
  400.         ' background visually. We are to use image of
  401.         ' picBackgd as the background.
  402.     If mTranspColor <> inColorToUse Then
  403.         For j = 0 To picH + 1
  404.             For i = 0 To picW + 1
  405.                 If inPic.Point(j, i) = mTranspColor Then
  406.                     inPic.PSet (j, i), vbWhite
  407.                 End If
  408.             Next i
  409.             DoEvents
  410.         Next j
  411.     End If
  412.     For j = 0 To picH + 1
  413.         For i = 0 To picW + 1
  414.             If inPic.Point(j, i) <> vbWhite Then
  415.                 inPic.PSet (j, i), inColorToUse
  416.             End If
  417.         Next i
  418.         DoEvents
  419.     Next j
  420. End Sub
  421. Private Sub cmdMethod1_Click()
  422.     On Error Resume Next
  423.     Me.MousePointer = vbHourglass
  424.     picMask.Cls
  425.     picMask.Picture = LoadPicture()
  426.     picReverseMaskedFgd.Cls
  427.     picReverseMaskedFgd.Picture = LoadPicture()
  428.     picReversedMask.Cls
  429.     picReversedMask.Picture = LoadPicture()
  430.     picProduct.Cls
  431.     picProduct.Picture = LoadPicture()
  432.     lblMask.Caption = "Mask"
  433.     lblReverseMaskedFgd.Caption = "Reverse-masked foreground"
  434.     picReverseMaskedFgd.Visible = True
  435.     picTransparent.Visible = False
  436.     lblReversedMask.Caption = "Reversed mask"
  437.     picReversedMask.Visible = True
  438.     picUnblockedFgd.Visible = False
  439.         ' (For method 1, we will superimpose on picProduct the
  440.         ' reverse masked foreground, not the picFgd itself, hence
  441.         ' we don't have to call doUnBlockForeGround as we do in
  442.         ' the case of method 2)
  443.         
  444.         ' Prepare picMask (get a replica of foregound image, then mask it)
  445.     mresult = BitBlt(picMask.hdc, 0, 0, picW, picH, _
  446.          picFgd.hdc, 0, 0, vbSrcCopy)
  447.          
  448.      ' Do masking
  449.     CreateMask picMask, vbBlack
  450.          ' Background picBackgd can readily be copied onto picProduct
  451.     BitBlt picProduct.hdc, 0, 0, picW, picH, picBackgd.hdc, 0, 0, vbSrcCopy
  452.     picProduct.Picture = picProduct.Image
  453.        ' Copy the mask onto the picProduct using the vbMergePaint opcode
  454.        ' to erase pixels corresponding to black parts of the mask.
  455.     BitBlt picProduct.hdc, 0, 0, picW, picH, picMask.hdc, 0, 0, vbMergePaint
  456.     picProduct.Picture = picProduct.Image
  457.     CreateReverseMaskedFgd
  458.        ' Copy the reverse masked Fgd image onto the masked background
  459.     BitBlt picProduct.hdc, 0, 0, picW, picH, picReverseMaskedFgd.hdc, _
  460.           0, 0, vbSrcAnd
  461.     picProduct.Picture = picProduct.Image
  462.     Me.MousePointer = vbDefault
  463. End Sub
  464. ' For creating reverse-masked foreground as an intermediary
  465. Private Sub CreateReverseMaskedFgd()
  466.        ' Make a reversed mask.
  467.     BitBlt picReversedMask.hdc, 0, 0, picW, picH, picMask.hdc, 0, 0, vbNotSrcCopy
  468.     picReversedMask.Picture = picReversedMask.Image
  469.        ' Copy picFgd to picReverseMaskedFgd
  470.     BitBlt picReverseMaskedFgd.hdc, 0, 0, picW, picH, picFgd.hdc, _
  471.         0, 0, vbSrcCopy
  472.     picReverseMaskedFgd.Picture = picReverseMaskedFgd.Image
  473.        ' Copy the earlier reversed mask onto the picRevserseMaskedFgd
  474.        ' using vbMergePaint opcode to erase part of the foreground
  475.        ' which corresponds to the black parts of that reversed mask.
  476.     BitBlt picReverseMaskedFgd.hdc, 0, 0, picW, picH, picReversedMask.hdc, _
  477.            0, 0, vbMergePaint
  478.     picReverseMaskedFgd.Picture = picReverseMaskedFgd.Image
  479. End Sub
  480. Private Sub cmdMethod2_Click()
  481.     On Error Resume Next
  482.     Me.MousePointer = vbHourglass
  483.     picMask.Cls
  484.     picMask.Picture = LoadPicture()
  485.     picUnblockedFgd.Cls
  486.     picUnblockedFgd.Picture = LoadPicture()
  487.     picTransparent.Cls
  488.     picTransparent.Picture = LoadPicture()
  489.     picProduct.Cls
  490.     picProduct.Picture = LoadPicture()
  491.     lblMask.Caption = "Mask"
  492.     lblReversedMask.Caption = "Unblocked foreground"
  493.     picUnblockedFgd.Visible = True
  494.     picReversedMask.Visible = False
  495.     lblReverseMaskedFgd.Caption = "Transparent bitmap"
  496.     picTransparent.Visible = True
  497.     picReverseMaskedFgd.Visible = False
  498.         ' For method 2, we have to check if entire foreground of picFgd
  499.         ' is painted; if so change it, but reflect the change in
  500.         ' picUnblockedFgd only (as after being made transparent it is
  501.         ' this one to be superimposed on picProduct)
  502.         '
  503.         ' Make a copy of picFgd for picUnblockedFgd first
  504.     mresult = BitBlt(picUnblockedFgd.hdc, 0, 0, picW, picH, _
  505.          picFgd.hdc, 0, 0, vbSrcCopy)
  506.     picUnblockedFgd.Picture = picUnblockedFgd.Image
  507.          
  508.         ' Unblock existing background as we are to use a
  509.         ' specified background as per picBackgd.
  510.     doUnBlockForeGround picFgd, picUnblockedFgd
  511.         ' Use the unblocked foreground to prepare picMask (get a
  512.         ' replica of foregound image, then mask it).
  513.       ' Make a copy of picUnblockedFgd for its masking
  514.     mresult = BitBlt(picMask.hdc, 0, 0, picW, picH, _
  515.          picUnblockedFgd.hdc, 0, 0, vbSrcCopy)
  516.       ' Do masking
  517.     CreateMask picMask, vbBlack
  518.     picMask.Picture = picMask.Image
  519.          ' Background picBackgd can readily be copied onto picProduct
  520.     BitBlt picProduct.hdc, 0, 0, picW, picH, picBackgd.hdc, _
  521.            0, 0, vbSrcCopy
  522.     picProduct.Picture = picProduct.Image
  523.        ' Copy the mask onto the picProduct using the vbMergePaint opcode
  524.        ' to erase pixels corresponding to black parts of the mask.
  525.     BitBlt picProduct.hdc, 0, 0, picW, picH, picMask.hdc, 0, 0, vbMergePaint
  526.     picProduct.Picture = picProduct.Image
  527.        ' Continue with other processes
  528.     Dim mColorAsTransparentr As Long
  529.        ' vbWhite as it is that part of area to become transparent in this case
  530.     mColorAsTransparentr = vbWhite
  531.     CreateTransparent picUnblockedFgd, picTransparent, _
  532.              mColorAsTransparentr
  533.              
  534.        ' Put the transparent picFgd on picProduct
  535.     BitBlt picProduct.hdc, 0, 0, picW, picH, picTransparent.hdc, _
  536.           0, 0, vbSrcAnd
  537.     picProduct.Picture = picProduct.Image
  538.     Me.MousePointer = vbDefault
  539.     DoEvents
  540. End Sub
  541. ' For creating a transparent bitmap as an intermediary
  542. Sub CreateTransparent(inpicSrc As PictureBox, inpicDest As PictureBox, _
  543.        inTransColor As Long)
  544.     On Error Resume Next
  545.     Dim mMaskDC As Long
  546.     Dim mMaskBmp As Long
  547.     Dim mTempMaskBMP As Long
  548.     Dim mMonoBMP As Long
  549.     Dim mMonoDC As Long
  550.     Dim mTempMonoBMP As Long
  551.     Dim mSrcHDC As Long, mDestHDC As Long
  552.     Dim w As Long, h As Long
  553.     w = inpicSrc.ScaleWidth
  554.     h = inpicSrc.ScaleHeight
  555.     mSrcHDC = inpicSrc.hdc
  556.     mDestHDC = inpicDest.hdc
  557.      ' Set back color of source pic and dest pic to
  558.      ' the desired transparent color
  559.     mresult = SetBkColor&(mSrcHDC, inTransColor)
  560.     mresult = SetBkColor&(mDestHDC, inTransColor)
  561.     ' Create a mask DC compatible with dest image
  562.     mMaskDC = CreateCompatibleDC(mDestHDC)
  563.     ' and a bitmap of its size
  564.     mMaskBmp = CreateCompatibleBitmap(mDestHDC, w, h)
  565.     ' Move that bitmap into mMaskDC
  566.     mTempMaskBMP = SelectObject(mMaskDC, mMaskBmp)
  567.     ' Meanwhile create another DC for mono bitmap by
  568.     '  setting nPlane and nbitCount both to 1.
  569.     mMonoDC = CreateCompatibleDC(mDestHDC)
  570.     '  and its bitmap, a mono one.
  571.     mMonoBMP = CreateBitmap(w, h, 1, 1, 0)
  572.     mTempMonoBMP = SelectObject(mMonoDC, mMonoBMP)
  573.     ' Copy source image to mMonoDC
  574.     mresult = BitBlt(mMonoDC, 0, 0, w, h, mSrcHDC, 0, 0, vbSrcCopy)
  575.         
  576.     ' Copy the mMonoDC into mMaskDC
  577.     mresult = BitBlt(mMaskDC, 0, 0, w, h, mMonoDC, 0, 0, vbSrcCopy)
  578.     'We don't need mMonoBMP any longer
  579.     mMonoBMP = SelectObject(mMonoDC, mTempMonoBMP)
  580.     mresult = DeleteObject(mMonoBMP)
  581.     mresult = DeleteDC(mMonoDC)
  582.     'Now copy source image to dest image with XOR
  583.     mresult = BitBlt(mDestHDC, 0, 0, w, h, mSrcHDC, 0, 0, vbSrcInvert)
  584.     'Copy the mMaskDC to dest image with AND
  585.     mresult = BitBlt(mDestHDC, 0, 0, w, h, mMaskDC, 0, 0, vbSrcAnd)
  586.     'Copy source image to dest image with XOR
  587.     BitBlt mDestHDC, 0, 0, w, h, mSrcHDC, 0, 0, vbSrcInvert
  588.     'Picture is there to stay
  589.     inpicDest.Picture = inpicDest.Image
  590.      
  591.     ' We don't need mask DC and bitmap.
  592.     mMaskBmp = SelectObject(mMaskDC, mTempMaskBMP)
  593.     mresult = DeleteObject(mMaskBmp)
  594.     mresult = DeleteDC(mMaskDC)
  595. End Sub
  596. ' Called only if Method 2 is deployed.
  597. Private Sub doUnBlockForeGround(inPic1 As PictureBox, inPic2 As PictureBox)
  598.     Dim mTranspColor
  599.     Dim i, j
  600.     mTranspColor = inPic1.Point(1, 1)
  601.     If mTranspColor <> vbWhite Then
  602.         For j = 0 To picH + 1
  603.             For i = 0 To picW + 1
  604.                 If inPic1.Point(j, i) = mTranspColor Then
  605.                       ' We keep inPic1 as it is, but change inPic2
  606.                       ' we are to use inPic2 if Method 2 is deployed.
  607.                     inPic2.PSet (j, i), vbWhite
  608.                 End If
  609.             Next i
  610.         Next j
  611.         DoEvents                    ' See something going on
  612.     End If
  613. End Sub
  614. Private Sub cmdClear_Click()
  615.     picMask.Cls
  616.     picMask.Picture = LoadPicture()
  617.     picReverseMaskedFgd.Cls
  618.     picReverseMaskedFgd.Picture = LoadPicture()
  619.     picReversedMask.Cls
  620.     picReversedMask.Picture = LoadPicture()
  621.     picProduct.Cls
  622.     picProduct.Picture = LoadPicture()
  623.     picUnblockedFgd.Cls
  624.     picUnblockedFgd.Picture = LoadPicture()
  625.     picTransparent.Cls
  626.     picTransparent.Picture = LoadPicture()
  627.     lblMask.Caption = ""
  628.     lblReverseMaskedFgd.Caption = ""
  629.     picReverseMaskedFgd.Visible = True
  630.     picTransparent.Visible = False
  631.     lblReversedMask.Caption = ""
  632.     picReversedMask.Visible = True
  633.     picUnblockedFgd.Visible = False
  634. End Sub
  635. Private Sub cmdExit_Click()
  636.     Unload Me
  637. End Sub
  638.